<template>
  <div class="page-seckill-seckill">
    <div class="seckill-list-mod">
      <!-- 搜索栏 -->
      <div class="search-bar">
        <el-form
          ref="test-form"
          :inline="true"
          class="search-form"
          :model="searchForm"

          @submit.prevent
        >
          <div class="input-row">
            <el-form-item
              :label="$t('prodList.shopName') + ':'"
              class="search-form-item"
            >
              <el-input
                v-model="searchForm.shopName"
                clearable
                :placeholder="$t('prodList.shopName')"
              />
            </el-form-item>
            <el-form-item
              :label="$t('groups.eventName') + ':'"
              class="search-form-item"
            >
              <el-input
                v-model="searchForm.seckillName"
                clearable
                :placeholder="$t('groups.eventName')"
              />
            </el-form-item>
            <el-form-item
              :label="$t('group.actStatus') + ':'"
              class="search-form-item"
            >
              <el-select
                v-model="searchForm.status"
                clearable
                :placeholder="$t('group.actStatus')"
              >
                <el-option
                  v-for="item in statusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <div
                class="default-btn primary-btn"
                @click="onSearch(true)"
              >
                {{ $t('shopFeature.searchBar.search') }}
              </div>
              <div
                class="default-btn"
                @click="clearSearch"
              >
                {{ $t('product.reset') }}
              </div>
            </el-form-item>
          </div>
        </el-form>
      </div>
      <!-- 搜索栏end -->
      <!-- 表格主体 -->
      <div class="main-container">
        <!-- 表格 -->
        <div class="table-con seckill-table">
          <el-table
            :data="dataList"
            header-cell-class-name="table-header"
            row-class-name="table-row-low"
            style="width: 100%"
          >
            <el-table-column
              fixed="left"
              prop="shopName"
              min-width="230"
              :label="$t('coupon.shopName')"
            >
              <template #default="scope">
                <span class="table-cell-text">{{ scope.row.shopName }}</span>
              </template>
            </el-table-column>

            <el-table-column
              fixed="left"
              prop="seckillName"
              min-width="230"
              :label="$t('seckill.activityName')"
            >
              <template #default="scope">
                <span class="table-cell-text">{{ scope.row.seckillName }}</span>
              </template>
            </el-table-column>

            <el-table-column
              min-width="300"
              :label="$t('seckill.prodInfo')"
            >
              <template #default="{row}">
                <div class="table-cell-con">
                  <div class="table-cell-image">
                    <ImgShow
                      :src="row.pic"
                      :img-alt="row.prodName"
                    />
                  </div>
                  <span class="table-cell-text">{{ row.prodName }}</span>
                </div>
              </template>
            </el-table-column>

            <el-table-column
              prop="startTime"
              min-width="240"
              :label="$t('coupon.startTime')"
            />

            <el-table-column
              prop="endTime"
              width="240"
              :label="$t('coupon.endTime')"
            />

            <el-table-column
              prop="maxNum"
              min-width="150"
              :label="$t('seckill.purchaseLimit')"
            />

            <el-table-column
              prop="status"
              min-width="150"
              :label="$t('group.actStatus')"
            >
              <template #default="scope">
                <div class="tag-text">
                  {{ [$t("seckill.loseEffectiveness"), $t("publics.normal"), $t("groups.offlineViolation"),
                      $t('product.pendingReview')]
                    [scope.row.status] }}
                </div>
              </template>
            </el-table-column>

            <el-table-column
              align="center"
              :label="$t('crud.menu')"
              width="230"
              fixed="right"
            >
              <template #default="scope">
                <div class="text-btn-con">
                  <div
                    class="default-btn text-btn"
                    @click="onAddOrUpdate(scope.row.seckillId)"
                  >
                    {{ $t("seckill.view") }}
                  </div>
                  <div
                    v-if="isAuth('platform:seckill:update') && scope.row.status === 1"
                    class="default-btn text-btn"
                    @click="offlineSeckillHandle(scope.row)"
                  >
                    {{ $t("coupon.violationOffShelf") }}
                  </div>
                  <div
                    v-if="isAuth('platform:seckill:update') && scope.row.status > 1"
                    class="default-btn text-btn"
                    @click="seckillAuditHandle(scope.row.seckillId)"
                  >
                    {{
                      scope.row.status === 2
                        ? $t("prodList.offShelfManage")
                        : $t("coupon.checkPending")
                    }}
                  </div>
                  <div
                    v-if="isAuth('platform:seckill:update')"
                    class="default-btn text-btn"
                    @click="onDelete(scope.row.seckillId, scope.row.status)"
                  >
                    {{ $t("coupon.delete") }}
                  </div>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 分页 -->
        <el-pagination
          v-if="dataList.length"
          :current-page="page.currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="onPageSizeChange"
          @current-change="onPageChange"
        />
      </div>
      <!-- 表格主体end -->
    </div>

    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />

    <!-- 商品审核弹窗 -->
    <offline-audit
      v-if="seckillAuditVisible"
      ref="seckillAuditRef"
      select-url="/platform/seckill/getOfflineHandleEventBySeckillId"
      update-url="/platform/seckill/auditSeckill"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import { isAuth } from '@/utils'
import { ElMessage, ElMessageBox } from 'element-plus'
import AddOrUpdate from './add-or-update.vue'
const statusList = [
  {
    label: $t('seckill.loseEffectiveness'),
    value: 0
  }, {
    label: $t('publics.normal'),
    value: 1
  }, {
    label: $t('groups.offlineViolation'),
    value: 2
  }, {
    label: $t('product.pendingReview'),
    value: 3
  }
]

onMounted(() => {
  getDataList()
})

let tempSearchForm = null // 保存上次点击查询的请求条件
const dataList = ref([])
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
// 头部搜索表单
const searchForm = reactive({
  shopName: null,
  seckillName: null,
  status: null
})
const dataListLoading = ref(false)
const getDataList = (pageParam, newData = false) => {
  dataListLoading.value = true
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm))
  }
  http({
    url: http.adornUrl('/platform/seckill/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.pageSize : pageParam.pageSize
        },
        tempSearchForm
      )
    )
  })
    .then(({ data }) => {
      data.records.forEach(element => {
        if (element.maxNum === -1) {
          element.maxNum = $t('seckill.unlimited')
        }
      })
      dataList.value = data.records
      page.total = data.total
      dataListLoading.value = false

      // 末尾页数据为空重新请求
      if (!dataList.value.length && page.value.currentPage > 1) {
        page.currentPage = 1
        getDataList()
      }
    })
}

const addOrUpdateVisible = ref(false)
const addOrUpdateRef = ref(null)
/**
 * 新增 / 修改
 * @param id
 */
const onAddOrUpdate = (id) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(id)
  })
}

const onDelete = (id) => {
  ElMessageBox.confirm($t('seckill.isDeleOper'), $t('text.tips'), {
    confirmButtonText: $t('coupon.confirm'),
    cancelButtonText: $t('coupon.cancel'),
    type: 'warning'
  })
    .then(() => {
      http({
        url: http.adornUrl('/platform/seckill/' + id),
        method: 'delete',
        data: http.adornData({})
      })
        .then(() => {
          ElMessage({
            message: $t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              getDataList(page)
            }
          })
        })
    })
}

/**
 * 刷新回调用
 */
const refreshChange = () => {
  getDataList(page)
}

const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}

/**
 * 下线活动
 * @param row
 */
const offlineSeckillHandle = (row) => {
  // '确定对活动[' + row.seckillName + ']进行下线操作？如是，请填写下线理由'
  let tips = $t('seckill.offlineTip')
  tips = tips.replace('ACTIVITYNAME', row.seckillName)

  ElMessageBox.prompt(tips, $t('text.tips'), {
    confirmButtonText: $t('coupon.confirm'),
    cancelButtonText: $t('coupon.cancel'),
    inputPattern: /\s\S+|S+\s|\S/, // 不能全为空格 空字符串
    inputErrorMessage: $t('seckill.offlineReasonTip'),
    inputValidator: (value) => {
      if (value.length > 100) {
        return $t('seckill.offlineReasonTip1')
      }
    }
  })
    .then((obj) => {
      http({
        url: http.adornUrl('/platform/seckill/offline'),
        method: 'post',
        data: http.adornData({
          handleId: row.seckillId,
          offlineReason: obj.value || null
        })
      })
        .then(() => {
          ElMessage({
            message: $t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              getDataList(page)
            }
          })
        })
    })
}

const seckillAuditVisible = ref(false)
const seckillAuditRef = ref(null)
/**
 * 弹窗处理
 * @param id
 */
const seckillAuditHandle = (id) => {
  seckillAuditVisible.value = true
  nextTick(() => {
    seckillAuditRef.value?.init(id)
  })
}

const clearSearch = () => {
  searchForm.shopName = null
  searchForm.seckillName = null
  searchForm.status = null
}

/**
 * 每页数量变更
 * @param val
 */
const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList()
}

/**
 * 页数变更
 * @param val
 */
const onPageChange = (val) => {
  page.currentPage = val
  getDataList()
}

</script>
<style lang="scss" scoped>
.page-seckill-seckill {
  :deep(.el-table__fixed) {
    height: auto !important;
    bottom: 16px !important;
    &::before {
      background-color: transparent !important;
    }
  }
  :deep(.el-table__fixed-right) {
    height: auto !important;
    bottom: 16px !important;
    &::before {
      background-color: transparent !important;
    }
  }
}
</style>
